<?php
/* ******************************
 *	Cartridge Parts List aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');
include('../../../includes/classes/category.tree.class.php');
$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $ident_id	= $_REQUEST['id'];
		$page		= GetPage(GetProduction($ident_id));
		
		$data		= array('page'	=> $page);
		
        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $local_id	= $_REQUEST['local_id'];
	    
	    $rResult = mysql_query("SELECT		`cartridge_identity`.`id`,
											`cartridge_parts_type`.`name` AS `type`,
	    									`production_category`.`name` AS `category`,
											`cartridge_identity`.`amount` AS `amount`
								FROM		`cartridge_identity`
								LEFT JOIN	`cartridge_parts_type`	ON `cartridge_identity`.`parts_type` = `cartridge_parts_type`.`id`
								LEFT JOIN	`production_category`	ON `cartridge_identity`.`parts_category` = `production_category`.`id` 
								WHERE		`cartridge_identity`.`production_id` = $local_id && `cartridge_identity`.`actived` = 1");
	    
		$data = array(
			"aaData"	=> array()
		);
				
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'save_ident':
		$ident_id	= $_REQUEST['id'];
		$local_id	= $_REQUEST['l_id'];
		
    	$category	= $_REQUEST['c'];
		$type		= $_REQUEST['t'];
		$amount		= $_REQUEST['a'];
		
		if($ident_id == ''){
			if(!CheckCategoryExist($local_id, $category)){
				AddIdentity($user_id, $local_id, $category, $type, $amount);
				if(!CheckTypeExist($local_id, $type)){
					AddToPriceList($user_id, $local_id, $type);
				}
			}else{
				$error = '"' . $category . '" უკვე არის სიაში!';
			}
		}else{
			SaveIdentity($ident_id, $user_id, $local_id, $category, $type, $amount);
		}

        break;
    case 'disable':
		$identity_id	= $_REQUEST['id'];
		$prod			= GetProduction($identity_id);
		if(!CheckTypeCount($prod['production_id'], $prod['parts_type'])){
			DisableToPriceList($prod['production_id'], $prod['parts_type']);
		}
		DisableCartridgeList($identity_id);
        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Cartridge Parts List Functions
 * ******************************
 */

function AddIdentity($user_id, $local_id, $category, $type, $amount)
{
	mysql_query("INSERT INTO `cartridge_identity`
					(`user_id`, `production_id`, `parts_type`, `parts_category`, `amount`) 
				 VALUES
					($user_id, $local_id, $type, $category, $amount)");
}

function AddToPriceList($user_id, $local_id, $type)
{

	$req	= mysql_query("	SELECT   DISTINCT `cartridge_pricelist`.`client_id`
							FROM    `cartridge_pricelist`
							WHERE   `cartridge_pricelist`.`production_id` = $local_id");
	
	while( $res = mysql_fetch_assoc($req)){
		mysql_query("	INSERT INTO `cartridge_pricelist`
							(`user_id`, `client_id`, `production_id`, `parts_type`, `price`)
						VALUES
							($user_id, $res[client_id], $local_id, $type, 0)");
	}
}

function SaveIdentity($ident_id, $user_id, $local_id, $category, $type, $amount)
{
	mysql_query("UPDATE
	    			`cartridge_identity`
				 SET
					`user_id`			= $user_id,
					`production_id`		= $local_id,
					`parts_type`		= $type,
					`parts_category`	= $category,
					`amount`			= $amount
				 WHERE
					`id` = $ident_id");
}

function DisableCartridgeList($ident_id)
{
    mysql_query("	UPDATE	`cartridge_identity`
				    SET		`actived`	= 0
				    WHERE	`id` = $ident_id");
}

function DisableToPriceList($prod_id, $type)
{
    mysql_query("	UPDATE	`cartridge_pricelist`
				    SET		`actived`	= 0
				    WHERE	`production_id` = $prod_id && `parts_type` = $type");
}

function CheckCategoryExist($local_id, $category)
{
    $res = mysql_fetch_assoc(mysql_query("SELECT	`id`
										  FROM		`cartridge_identity`
										  WHERE		`production_id` = $local_id && `parts_category` = $category && `actived` = 1"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}

function CheckTypeExist($prod_id, $type)
{
    $res = mysql_fetch_assoc(mysql_query("SELECT	`id`
										  FROM		`cartridge_pricelist`
										  WHERE		`production_id` = $prod_id && `parts_type` = $type && `actived` = 1"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}

function CheckTypeCount($prod_id, $type)
{
	$req = mysql_query("SELECT	`id`
						FROM	`cartridge_identity`
						WHERE	`production_id` = $prod_id && `parts_type` = $type && `actived` = 1");
	if (mysql_num_rows($req) > 1){
		return true;
	}
	
	return false;
}

function GetProductID($prod_name) 
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
    $res = mysql_fetch_assoc(mysql_query("SELECT 	`id`
										  FROM 		`production`
										  WHERE 	`name` = '$prod_name'"));
	return $res['id'];
}

function GetIdentityProductID($identity_id)
{
	$res = mysql_fetch_assoc(mysql_query("	SELECT 	`production_id`
											FROM 	`cartridge_identity`
											WHERE 	`id` = $identity_id"));
	return $res['production_id'];
}

function GetCategory($point)
{
	$data = '';

	$data = '<option value="0" selected="selected"></option>';
	$tree = new CategoryTree($point);
	$tree->SetParent(8);
	$data .= $tree->GetData();

	return $data;
}

function GetPartType($point)
{
	$data	= '';
	
	$req	= mysql_query("	SELECT	`id`,
									`name`
							FROM	`cartridge_parts_type`
							WHERE	`actived` = 1");

	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}

	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function GetProduction($ident_id)
{
    $res = mysql_fetch_assoc(mysql_query("	SELECT	`id`,
    												`production_id`,
													`parts_category`,
													`parts_type`,
													`amount`
											FROM	`cartridge_identity`
											WHERE	`id` = $ident_id"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>		    
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="type">ნაწილის ტიპი</label></td>
					<td>
						<select id="type" class="idls">' . GetPartType($res[parts_type]) . '</select>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="ident_category">კატეგორია</label></td>
					<td>
						<select id="ident_category" class="idls">' . GetCategory($res[parts_category]) . '</select>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="amount">რაოდენობა</label></td>
					<td>
						<input type="text" id="amount" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['amount'] . '" />
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="identity_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}

?>